package com.ruoyi.recycle.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.recycle.domain.Coupon;
import com.ruoyi.recycle.domain.SalesmanManagement;
import com.ruoyi.recycle.domain.ShopManagement;
import com.ruoyi.recycle.dto.ShopManagementDTO;
import com.ruoyi.recycle.service.CouponService;
import com.ruoyi.recycle.service.IShopManagementService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.List;

/**
 * 引流门店管理Controller
 *
 * @author Wubk
 * @date 2022-05-21
 */
@RestController
@RequestMapping("/shop")
@Api(tags = "引流门店管理")
public class ShopManagementController extends BaseController {
    @Autowired
    private IShopManagementService shopManagementService;
    @Autowired
    private CouponService couponService;

    /**
     * 查询引流门店管理列表
     */
    @RequiresPermissions("user:shop:list")
    @GetMapping("/list")
    public TableDataInfo list(ShopManagement shopManagement) {
        startPage();
        List<ShopManagement> list = shopManagementService.selectShopManagementList(shopManagement);
        return getDataTable(list);
    }

    /**
     * 分页条件查询
     */
    @PostMapping("/shopPageQuery")
    @ApiOperation("分页条件查询")
    public IPage<ShopManagement> shopPageQuery(@RequestBody ShopManagementDTO queryDTO) throws Exception{
        final ShopManagement shop = new ShopManagement();
        BeanUtils.copyProperties(queryDTO, shop);
        final LambdaQueryWrapper<ShopManagement> wrapper = Wrappers.lambdaQuery(shop);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        SimpleDateFormat old = new SimpleDateFormat("yyyy-MM-dd");
        if (queryDTO.getSearchTimeBegin() != null) {
            wrapper.ge(ShopManagement::getCreatedTime, simpleDateFormat.parse(old.format(queryDTO.getSearchTimeBegin()) + " 00:00:00"));
        }
        if (queryDTO.getSearchTimeEnd() != null) {
            wrapper.le(ShopManagement::getCreatedTime, simpleDateFormat.parse(old.format(queryDTO.getSearchTimeEnd()) + " 23:59:59"));
        }
        wrapper.orderByDesc(ShopManagement::getCreatedTime);
        return shopManagementService.page(new Page<ShopManagement>()
                .setCurrent(queryDTO.getCurrentPage())
                .setSize(queryDTO.getPageSize()), wrapper);
    }

    /**
     * 导出引流门店管理列表
     */
    @RequiresPermissions("user:shop:export")
    @Log(title = "引流门店管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ApiOperation("导出引流门店管理列表")
    public void export(HttpServletResponse response, ShopManagement shopManagement) {
        List<ShopManagement> list = shopManagementService.selectShopManagementList(shopManagement);
        ExcelUtil<ShopManagement> util = new ExcelUtil<ShopManagement>(ShopManagement.class);
        util.exportExcel(response, list, "引流门店管理数据");
    }

    /**
     * 获取引流门店管理详细信息
     */
    @GetMapping(value = "/{id}")
    @ApiOperation("获取引流门店管理详细信息")
    @ApiImplicitParam(name = "id", value = "id", dataType = "Long")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(shopManagementService.selectShopManagementById(id)).put("couponList",couponService.list(new LambdaQueryWrapper<Coupon>().eq(Coupon::getShopId,id)));
    }

    /**
     * 新增引流门店管理
     */
    //@RequiresPermissions("user:shop:add")
    @Log(title = "引流门店管理", businessType = BusinessType.INSERT)
    @PostMapping
    @ApiOperation("新增引流门店管理")
    public AjaxResult add(@RequestBody ShopManagement shopManagement) {
        return toAjax(shopManagementService.insertShopManagement(shopManagement));
    }

    /**
     * 修改引流门店管理
     */
   @RequiresPermissions("user:shop:edit")
   @Log(title = "引流门店管理", businessType = BusinessType.UPDATE)
    @PutMapping
    @ApiOperation("修改引流门店管理")
    public AjaxResult edit(@RequestBody ShopManagement shopManagement) {
        return toAjax(shopManagementService.updateShopManagement(shopManagement));
    }

    /**
     * 删除引流门店管理
     */
    @RequiresPermissions("user:shop:remove")
    @Log(title = "引流门店管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    @ApiOperation("删除引流门店管理")
    @ApiImplicitParam(name = "ids", value = "id集合", dataType = "List")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(shopManagementService.deleteShopManagementByIds(ids));
    }

    @ApiOperation("修改门店的状态")
    @PostMapping("/changeStatus")
    @ApiImplicitParams({@ApiImplicitParam(name = "id", value = "门店Id", dataType = "Long"),
            @ApiImplicitParam(name = "status", value = "状态", dataType = "String")})
    public AjaxResult changeStatus(@RequestParam Long id, @RequestParam String status) {
        return shopManagementService.changeStatus(id, status);
    }
}
